package io.dbw.common.utils;

import io.dbw.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author lxc
 * @description:
 * @date 2021/7/6 14:54
 */
@Slf4j
@Component
public class RestTemplateHttp {
    /**
     * httpGetForObject不带参数
     * @param url
     * @param t
     * @param <T>
     * @return
     */
    public <T> T  httpGetForObject(String url,Class<T> t){
        RestTemplate restTemplate = new RestTemplate();
        T result = restTemplate.getForObject(url,t);
        return result;
    }
    /**
     * httpGetForEntity不带参数
     * @param url
     * @param t
     * @param <T>
     * @return
     */
    public <T> T httpGetForEntity(String url,Class<T> t) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<T> responseEntity = restTemplate.getForEntity(url, t);
        HttpStatus statusCode = responseEntity.getStatusCode();
        if (statusCode.value() == HttpStatus.OK.value()) {
            return responseEntity.getBody();
        } else {
            log.info("httpGetForEntity is url:", url);
            log.error("httpGetForEntity is fail,fail statusCode:{0}", statusCode.toString());
            throw new BusinessException("201", "httpGetForEntity调用失败");
        }
    }

    /**
     * httpGetForObject带参数,map接收
     * @param url
     * @param t
     * @param <T>
     * @return
     */
    public <T> T httpGetForObject(String url, Class<T> t, Map<String, Object> map) {
//        String url = "api/v1/form/get?name={name}&age={age}&phone={phone}";
        RestTemplate restTemplate = new RestTemplate();
        T result = restTemplate.getForObject(url, t, map);
        return result;
    }

    /**
     * httpPostForEntity不带参数
     * @param url
     * @param t
     * @param <T>
     * @return
     */
    public <T> T httpPostForEntity(String url, Class<T> t) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(headers);
        ResponseEntity<T> response = restTemplate.postForEntity(url, request, t);
        HttpStatus statusCode = response.getStatusCode();
        if (statusCode.value() == HttpStatus.OK.value()) {
            return response.getBody();
        } else {
            log.info("httpPostForEntity is url:", url);
            log.error("httpPostForEntity is fail,fail statusCode:{0}", statusCode.toString());
            throw new BusinessException("201", "httpPostForEntity调用失败");
        }
    }


    /**
     * httpPostForEntity带参数， 参数为@RequestParam接收
     * @param url
     * @param t
     * @param <T>
     * @return
     */
    public <T> T httpPostForEntityRequestParam(String url, Class<T> t, MultiValueMap<String, Object> map) {
        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<T> response = restTemplate.postForEntity(url, request, t);
        HttpStatus statusCode = response.getStatusCode();
        if (HttpStatus.OK.value()==statusCode.value()  ) {
            return response.getBody();
        } else {
            log.info("httpPostForEntity map is url:", url);
            log.error("httpPostForEntity map is fail,fail statusCode:{0}", statusCode.toString());
            throw new BusinessException("201", "httpPostForEntity调用失败");
        }
    }

    /**
     * httpPostForEntityRequestBody,参数为@RequestBody
     * @param url
     * @param t
     * @param <T>
     * @return
     */
    public <T> T httpPostForEntityRequestBody(String url, Class<T> t,Map<String,Object> map) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(map,headers);
        ResponseEntity<T> response = restTemplate.postForEntity( url, request ,t);
        HttpStatus statusCode = response.getStatusCode();
        if (HttpStatus.OK.value()==statusCode.value()  ) {
            return response.getBody();
        } else {
            log.info("httpPostForEntityRequestBody is url:", url);
            log.error("httpPostForEntityRequestBody is fail,fail statusCode:{0}", statusCode.toString());
            throw new BusinessException("201", "httpPostForEntity调用失败");
        }
    }
}
